//
//  VisitorView.swift
//  Webo10
//
//  Created by ChengXiwu on 2017/7/28.
//  Copyright © 2017年 ChengXiwu. All rights reserved.
//

import UIKit
import SnapKit

class VisitorView: UIView{
    
    // MARK: - 设置视图信息
    func setupInfo(imageName: String?, title: String){
        messageLabel.text = title
        
        // 如果图片名称是nil,说明是首页,直接返回
        guard let imgName = imageName else {
            startAnim()
            return
        }
        iconView.image = UIImage(named: imgName)
        // 隐藏小房子
        homeIconView.isHidden = true
        // 将遮罩图片移到后面
        sendSubview(toBack: maskIconView)
    }
    
    /// 设置动画
    func startAnim(){
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2 * Double.pi
        anim.repeatCount = MAXFLOAT
        anim.duration = 20
        
        // 用在不断重复的动画上,当动画绑定的图层对应的视图被销毁,动画自动被销毁
        anim.isRemovedOnCompletion = false
        // 添加到图层
        iconView.layer.add(anim, forKey: nil)
    }
    
    // MARK: - 构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        // 阻止SB使用当前自定义视图
        // fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
        
        setupUI()
    }
    
    // MARK: - 懒加载控件
    // 图标
    fileprivate lazy var iconView: UIImageView = UIImageView(imageName: "visitordiscover_feed_image_smallicon")
    
    // 遮罩
    fileprivate lazy var maskIconView: UIImageView = UIImageView(imageName: "visitordiscover_feed_mask_smallicon")
    
    // 小房子
    fileprivate lazy var homeIconView: UIImageView = UIImageView(imageName: "visitordiscover_feed_image_house")
    
    // 标签
    fileprivate lazy var messageLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.darkGray
        label.font = UIFont.systemFont(ofSize: 14)
        label.numberOfLines = 0
        label.textAlignment = NSTextAlignment.center
        return label
    }()
    
    // 注册按钮
    lazy var registerButton: UIButton = UIButton(title: "注册", foreColor: UIColor.orange, backgroundImageName: "common_button_white_disable")
    // 登录按钮
    lazy var loginButton: UIButton = UIButton(title: "登录", foreColor: UIColor.darkGray, backgroundImageName: "common_button_white_disable")
    
    
}

extension  VisitorView{
    
    fileprivate func setupUI(){
        // 添加控件
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeIconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        // translatesAutoresizingMaskIntoConstraints默认是true,支持使用setFrame方式设置控件位置,false支持使用自动布局来设置,使用纯代码进行自动布局必不可少的一句
        for v in subviews{
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        
        // 设置自动布局
        iconView.snp.makeConstraints{ (make) in
            make.centerX.equalTo(self.snp.centerX)
            make.centerY.equalTo(self.snp.centerY).offset(-60)
        }
        homeIconView.snp.makeConstraints { (make) in
            make.center.equalTo(iconView.snp.center)
        }
        messageLabel.snp.makeConstraints { (make) in
            make.centerX.equalTo(iconView.snp.centerX)
            make.top.equalTo(iconView.snp.bottom).offset(16)
            make.width.equalTo(224)
            make.height.equalTo(36)
        }
        registerButton.snp.makeConstraints { (make) in
            make.left.equalTo(messageLabel.snp.left)
            make.top.equalTo(messageLabel.snp.bottom).offset(16)
            make.width.equalTo(100)
            make.height.equalTo(36)
        }
        loginButton.snp.makeConstraints { (make) in
            make.right.equalTo(messageLabel.snp.right)
            make.top.equalTo(registerButton.snp.top)
            make.size.equalTo(registerButton.snp.size)
        }
        maskIconView.snp.makeConstraints { (make) in
            make.top.equalTo(self.snp.top)
            make.left.equalTo(self.snp.left)
            make.right.equalTo(self.snp.right)
            make.bottom.equalTo(registerButton.snp.bottom)
        }
        
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)
    }
}
